// pages/changePassword/changePassword.js
var common = require("../../utils/Conmon.js");
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    NowPwd: '',
    NewPwd: '',
    NewPwdSure: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    common.pageInit(function() {});
  },

  /**
   * 原密码输入
   */
  bindNowPwdInput: function(event) {
    this.setData({
      NowPwd: event.detail.value
    })
  },

  /**
   * 新密码输入
   */
  bindNewPwdInput: function(event) {
    this.setData({
      NewPwd: event.detail.value
    })
  },

  /**
   * 新密码重新确认输入
   */
  bindPwdSureInput: function(event) {
    this.setData({
      NewPwdSure: event.detail.value
    })
  },

  /**
   * 修改密码提交
   */
  bindSaveClickTap: function(event) {
    var that = this;
    if (that.data.NowPwd.length == 0 || that.data.NowPwd == '') {
      wx.showModal({
        title: '警告',
        content: '请输入原密码',
        showCancel: false
      })
      return;
    }
    if (that.data.NewPwd.length < 6) {
      wx.showModal({
        title: '警告',
        content: '密码长度不能小于6位',
        showCancel: false
      })
      return;
    }
    if (that.data.NewPwd != that.data.NewPwdSure) {
      wx.showModal({
        title: '警告',
        content: '两次密码不一致,重输入',
        showCancel: false
      })
      return;
    }
    var param = {
      UserID: wx.getStorageSync('UserInfo').UserID,
      CustID: appData.custID,
      Password: that.data.NewPwd,
      OldPwd: that.data.NowPwd
    };
    wx.showLoading({
      title: '保存中...',
      mask: true
    });
    wx.request({
      url: appData.apiurl + 'member/api/MemberCenter/UpdatePwd',
      method: 'POST',
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        if (res.statusCode == 200 && res.data.code == 1200) {
          wx.hideLoading();
          wx.redirectTo({
            url: '/pages/my/my',
          })
          console.log(res.data)
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000,
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '错误',
          content: '操作失败',
          showCancel: false
        })
        console.log(res.data)
      },
      complete: function(res) {
        console.log(res.msg)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})